﻿package com.wongoing.sys.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wongoing.base.BaseServiceImpl;
import com.wongoing.base.PageResult;
import com.wongoing.bus.util.Constants;
import com.wongoing.sys.dao.SysBasSliderDao;
import com.wongoing.sys.model.SysBasSlider;
import com.wongoing.sys.service.SysBasSliderService;

@Service("sysBasSliderService")
public class SysBasSliderServiceImpl extends BaseServiceImpl<SysBasSlider> implements SysBasSliderService {
	private SysBasSliderDao dao;

	@Autowired
	public void setDao(SysBasSliderDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}

	/**
	 * 功能说明：通过PageResult获取公司分页数据
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月13日 下午8:43:21
	 * @param pageResult 分页查询对象，包含查询条件
	 * @return 返回分页查询对象，包含页面数据
	 */
	public PageResult<SysBasSlider> getSliderListPageResult(PageResult<SysBasSlider> pageResult) {
		pageResult.getParam().put("offset", pageResult.getPageSize() * (pageResult.getPageCurrent() - 1));
		pageResult.getParam().put("limit", pageResult.getPageSize());
		if (!"".equals(pageResult.getOrderField())) { pageResult.getParam().put("orderColumn", pageResult.getOrderField()); }
		pageResult.getParam().put("orderTurn", pageResult.getOrderDirection());
		List<SysBasSlider> data = dao.getSliderListByParam(pageResult.getParam());
		pageResult.setList(data);
		int totalSize = dao.countOfSliderListByParam(pageResult.getParam());
		pageResult.setTotal(totalSize);
		return pageResult;
	}
	/**
	 * 
	 * 功能说明：储存上传图片地址
	 * 修改说明：
	 * @author zhaoxihao
	 * @date 2017-7-27 上午8:49:02
	 * @param id	主键id
	 * @param picture	图片地址
	 * @return
	 */
	
	public boolean setSliderPicture(Integer id, String picture) {
		SysBasSlider slider = dao.getByPrimaryKey(id);
		slider.setImageUrl(picture);
		int flag = dao.updateByPrimaryKey(slider);
		switch (flag) {
		case 1:
			return true;
		case 0:
			return false;
		default:
			break;
		}
		return true;
	}

	@Override
	public List<SysBasSlider> carousel() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

		List<SysBasSlider> sysBasSliderList = redisTemplate.execute(new RedisCallback<List<SysBasSlider>>() {

			@Override
			public List<SysBasSlider> doInRedis(RedisConnection connection) throws DataAccessException {
				 RedisSerializer<String> serializer = getRedisSerializer();  
				 byte[] key = serializer.serialize("homeSliderList");  
                 byte[] value = connection.get(key);  
                 if (value == null) {  
                    return null;  
                 }  
                 String text = serializer.deserialize(value);  
                 return JSON.parseArray(text,SysBasSlider.class);
			}
			
		});
		
		return sysBasSliderList;
	}

	@Override
	public List<SysBasSlider> addCarousel() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("sliderTypeId", 1);
		param.put("status", Constants.STATUS_NORMAL);
		List<SysBasSlider> sliderList = getByParam(param); 
		String s = JSON.toJSONString(sliderList);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String> serializer = getRedisSerializer();  
                byte[] key  = serializer.serialize("homeSliderList");  
                byte[] name = serializer.serialize(s);  
                connection.set(key, name);
                String text = serializer.deserialize(connection.get(key));  
                return true;  
			}
			
		});
		return sliderList;
	}

}
